package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;


import objects.AccessPoint;
import objects.Point;

public class AccessPointsDAO {
	
	/**
	 * Saves an arrayList of access points in the database
	 * @param Aps
	 * @return
	 */
	public static int create(ArrayList<AccessPoint> Aps) {
		
		Connection con = null;
		int nbApsAdded = 0;
		
		try {
			Class.forName("org.hsqldb.jdbcDriver").newInstance();
			con = DriverManager.getConnection(DatabaseConnection.getDataServerUrl(), DatabaseConnection.getLogin(), DatabaseConnection.getPassword());

			CallableStatement cs = con.prepareCall("DELETE FROM AP");
			cs.executeUpdate();
			
			cs = con.prepareCall("ALTER SEQUENCE PUBLIC.AP_SEQ RESTART WITH 0");
			cs.executeUpdate();
			
			cs = con.prepareCall("INSERT INTO AP (ID_AP, ABSCISSA, ORDINATE, HEIGHT, MAC, FREQUENCY, ANTENNA_GAIN, OUTPUT_POWER)" +
					"VALUES (NEXT VALUE FOR AP_SEQ, ?, ?, ?, ?, ?, ?, ?)");

			for (AccessPoint  ap : Aps) {
				cs.setFloat(1, ap.getCoordinates().getX());
				cs.setFloat(2, ap.getCoordinates().getY());
				cs.setFloat(3, ap.getCoordinates().getZ());
				cs.setString(4, ap.getMacAdress());
				cs.setDouble(5, ap.getFrequency());
				cs.setFloat(6, ap.getAntennaGain());
				cs.setFloat(7,ap.getOutputPower());
				nbApsAdded += cs.executeUpdate();
			}
		}
		catch (Exception e) {
			System.out.println("Failure : " + e); 
			System.exit(-1);
		}
		finally {
			if (con != null) {
				try {
					con.close();
				} 
				catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		return nbApsAdded;
	}
	
	/**
	 * 
	 * @return the access points in the database
	 */
	public static HashMap<String, AccessPoint> read() {
		
		Connection con = null;
		HashMap<String, AccessPoint> accessPoints = new HashMap<String, AccessPoint>();
		
		try {
			Class.forName("org.hsqldb.jdbcDriver").newInstance();
			con = DriverManager.getConnection(DatabaseConnection.getDataServerUrl(), DatabaseConnection.getLogin(), DatabaseConnection.getPassword());

			CallableStatement cs = con.prepareCall("SELECT ID_AP, ABSCISSA, ORDINATE, HEIGHT, MAC, FREQUENCY, ANTENNA_GAIN, OUTPUT_POWER FROM AP");

			ResultSet rs = cs.executeQuery();
			
			while (rs.next()) {
				AccessPoint newAp = new AccessPoint();
				newAp.setId(rs.getInt("ID_AP"));
				newAp.setCoordinates(new Point(rs.getFloat("ABSCISSA"), rs.getFloat("ORDINATE"), rs.getFloat("HEIGHT")));
				newAp.setMacAdress(rs.getString("MAC"));
				newAp.setFrequency(rs.getLong("FREQUENCY"));
				newAp.setAntennaGain(rs.getFloat("ANTENNA_GAIN"));
				newAp.setOutputPower(rs.getFloat("OUTPUT_POWER"));
				accessPoints.put(newAp.getMacAdress(), newAp);
			}
		}
		catch (Exception e) {
			System.out.println("Failure : " + e.getMessage()); 
			System.exit(-1);
		}
		finally {
			if (con != null) {
				try {
					con.close();
				} 
				catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}		
		return accessPoints;
	}
}
